package net.exoweb.samples
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    import net.exoweb.animate.actions.*;
    import net.exoweb.animate.animaters.*;
    import net.exoweb.animate.common.*;
    import net.exoweb.animate.events.*;
    import net.exoweb.animate.objects.*;

    [SWF(width="640", height="480", backgroundColor="#000000")]
    public class AnimaterTester extends Sprite
    {
        private var front:Sprite = new Sprite();
        private var timer:Time;
        private var animaters:Array;
        private var size:Number = 4;

        public function AnimaterTester()
        {
            animaters = new Array();
            timer = new Time();
            for(var i:int=0; i<30; i++)
            {
                var animater:Animater = new Animater();

                var polygon:Polygon = new Polygon(2, 0xFFFFFF, 0x0000FF, 0.25, BlendMode.ADD);
                polygon.x = getRandomWidth();
                polygon.y = getRandomHeight();
                polygon.alpha = 0.5;
                var points:Array = [new Vector(-20, -10),
                                    new Vector(20, -10),
                                    new Vector(20, 10),
                                    new Vector(-20, 10)];
                polygon.points = points;
                polygon.scaleX = getRandomSize();
                polygon.scaleY = getRandomSize();
                animater.addObject(polygon);

                var scale:Scale = new Scale(getRandomSize(), getRandomSize(), 1);
                scale.addEventListener(ActionEvent.ACTION_COMPLETE, goonScaleXY);
                animater.addAction(scale);

                var v:Vector = new Vector(0, 0);
                v.x = getRandomWidth();
                v.y = getRandomHeight();
                var trans:UniformTranslation = new UniformTranslation(v, 10);
                trans.addEventListener(ActionEvent.ACTION_COMPLETE, goonTrans);
                animater.addAction(trans);

                animaters.push(animater);
                addChild(animater);
            }
            for(i=0; i<10; i++)
            {
                animater = new Animater();

                var circle:Circle = new Circle(1, 0xFFFFFF, 10, 0xFFFFFF, 0.2, BlendMode.ADD);
                circle.x = getRandomWidth();
                circle.y = getRandomHeight();
                circle.alpha = 0.5;
                var x:Number = getRandomSize();
                circle.scaleX = x;
                circle.scaleY = x;
                animater.addObject(circle);

                x = getRandomSize();
                scale = new Scale(x, x, 1);
                scale.addEventListener(ActionEvent.ACTION_COMPLETE, goonScaleXX);
                animater.addAction(scale);

                v.x = getRandomWidth();
                v.y = getRandomHeight();
                trans = new UniformTranslation(v, 10);
                trans.addEventListener(ActionEvent.ACTION_COMPLETE, goonTrans);
                animater.addAction(trans);

                animaters.push(animater);
                addChild(animater);
            }

            var front:Sprite = new Sprite();
            front.blendMode = BlendMode.OVERLAY;
            var colors:Array = [0x00FF00, 0xFF0000];
            var alphas:Array = [1, 1];
            var ratios:Array = [0, 190];
            var matr:Matrix = new Matrix();
            matr.createGradientBox(640, 480, -10, -320, -240);
            front.graphics.beginGradientFill(GradientType.LINEAR,
                                             colors, alphas,
                                             ratios, matr,
                                             SpreadMethod.REFLECT,
                                             InterpolationMethod.RGB, 0.75);
            front.graphics.drawRect(0, 0, 640, 480);
            front.graphics.endFill();
            addChild(front);

            addEventListener(Event.ENTER_FRAME, update);
            addEventListener(MouseEvent.MOUSE_DOWN, down);
        }

        public function down(e:Event):void
        {
            stage.displayState = "fullScreen";
            trace("Event: Full screen");
        }

        public function update(e:Event):void
        {
            var t:Number = timer.interval();
            timer.next();
            for(var i:int=0; i<animaters.length; i++)
            {
                animaters[i].update(t);
            }
        }

        public function goonScaleXY(e:Event):void
        {
            e.target.x = getRandomSize();
            e.target.y = getRandomSize();
        }

        public function goonScaleXX(e:Event):void
        {
            var x:Number = getRandomSize();
            e.target.x = x;
            e.target.y = x;
        }

        public function goonTrans(e:Event):void
        {
            e.target.toPos.x = getRandomWidth();
            e.target.toPos.y = getRandomHeight();
        }

        public function getRandomWidth():Number
        {
            return 100 + Math.random() * 450;
        }

        public function getRandomHeight():Number
        {
            return 100 + Math.random() * 300;
        }

        public function getRandomSize():Number
        {
            return Math.random() * 4;
        }
    }
}
